;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Purchase.Supplier", [])
        .controller("SupplierCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", "$q", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService, $q) {
            $scope.activeFlag = [true, false, false, false, false];
            $scope.markInfo = {
                allChecked: false
            };
            $scope.AllExport = {};
            $scope.isCHYDlsh = false
            //仓库库存勾选的数据
            $scope.CKCheck = []            
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/marketingActivity/supplierPriceExportSelected"),
                sectionParam: {
                    list: [],
                    supplierOrgId:''
                },
                allUrl: ybHttp.RequestUrl("device", "/marketingActivity/supplierPriceExportAll")
            };             
            $scope.boundObj = {};
            $scope.orgInfo = {};
            $scope.submitted = false;
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.dataList = [];
            $scope.detailInfo = {
                isDirect:0,
                locked: 0
            };
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };
            $scope.tbConf = {
                fixedWidth: 340,
                scrollWidth: 1140,
                tableCol: [
                    {
                        type: "",
                        name: "供货商代码",
                        width: 150,
                        dataField: "supplierCode",
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "供货商名称",
                        width: 150,
                        dataField: "companyName",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    /* {
                        type: "",
                        name: "供货商orgId",
                        width: 150,
                        dataField: "supplierOrgId",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }, */
                    {
                        type: "",
                        name: "分类名称",
                        width: 100,
                        dataField: "supplierClassName",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "供货商简称",
                        width: 150,
                        dataField: "shortName",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "地址",
                        width: 150,
                        dataField: "address",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "联系人",
                        width: 100,
                        dataField: "contact",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "联系方式",
                        width: 100,
                        dataField: "contactTel",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "传真",
                        width: 100,
                        dataField: "fax",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "邮编",
                        width: 100,
                        dataField: "zip",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建日期",
                        dataField: "createDateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },{
                        type: "",
                        name: "审核状态",
                        dataField: "applyStatusDesc",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 320,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            var btnPermissionCode = {
                supplierAdd: window.ActionCode.supplierAdd,
                supplierEdit: window.ActionCode.supplierEdit,
                supplierDel: window.ActionCode.supplierDel,
                supplierBind: window.ActionCode.supplierBind,
                supplierUpdate: window.ActionCode.supplierUpdate,
                priceStock: window.ActionCode.priceStock,
                supplierInvite:window.ActionCode.supplierInvite,
                supplierPriceStockExport:window.ActionCode.supplierPriceStockExport,
                supplierPriceStockExportAll:window.ActionCode.supplierPriceStockExportAll,
            };

            $scope.btnPermission = {
                supplierAdd: true,
                supplierEdit: true,
                supplierDel: true,
                supplierBind: true,
                supplierUpdate: true,
                supplierInvite:true,
                priceStock:true,
                supplierPriceStockExport:true,
                supplierPriceStockExportAll:true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    scrollLeft: {left: $scope.tbConf.fixedWidth + "px"},
                    scrollLeftWidth: {width: $scope.tbConf.fixedWidth + "px"}
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.tabFlag = {};
                $scope.boundObj = {};
                $scope.orgInfo = {};
                $scope.detailInfo = {
                    isDirect: 0,
                    locked: 0
                };
            }

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                var service = "device";
                var reqUrl = "/companySupplier/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.createDateShow = $filter("YbDate")(item.createDate);
                        item.showPriceBtn = true
                        item.supplierClassName = $scope.supplierClassMap[item.supplierClassId];
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.bound = function () {
                $scope.changeTab(2);
            };
            $scope.getInfo = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = 'device';
                var reqUrl = '/company/itemByInvitation';
                ybHttp.Get(service, reqUrl, $scope.boundObj).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.orgInfo = resp.value;
                })
            };
            $scope.doBound = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/companySupplier/bound";
                ybHttp.Post(service, reqUrl, $scope.boundObj).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                    $scope.changeTab(0);
                })
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };
            $scope.invite = function (id) {
                var service = "device";
                var reqUrl = "/companySupplier/invite";
                ybHttp.Post(service, reqUrl, {id: id}).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowMessage('邀请码为' + resp.value, MESG_TYPE.Success);
                });
            };
            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
            };
            $scope.addData = function () {
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                $scope.changeTab(1);
            };

            $scope.delData = function (delType, id) {
                var service = "device";
                var reqUrl = "/companySupplier/remove";
                var param = {};
                param.ids = [];
                if (delType === "single") {
                    param.ids.push(id);
                }
                if (delType === "batch") {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        if (!!$scope.dataList[i].isChecked) {
                            param.ids.push($scope.dataList[i].id);
                        }
                    }
                    if (param.ids.length === 0) {
                        ybWidget.ShowMessage('请选择需要删除的行');
                        return;
                    }
                }
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                }, function () {

                })
            };

            $scope.editData = function (item) {
                $scope.detailInfo = item;
                $scope.changeTab(1);
                $scope.tabFlag.type = 1;
                $scope.tabFlag.name = tabFlag[1];
            };

            function save(service, reqUrl, param) {
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp.value;
                });
            }

            $scope.supplierClassMap = {
                '11': '供货商供货',
                '22': '外协仓供货',
            };

            $scope.saveData = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/companySupplier/insert";
                if ($scope.tabFlag.type === 1) {
                    reqUrl = "/companySupplier/update";
                }
                save(service, reqUrl, $scope.detailInfo).then(function (data) {
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                });
            };

            // 仓库库存 Start
            $scope.warehouseStock = {
                supplierOrgId: '',
                markInfo: {
                    curRow: 0
                },
                pageInfo: {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                },
                dataList: [],
                condition: {},
                toolbarConfig: {
                    showAdvanceSearch: false,
                    showColConfig: false,
                    showMateriel: false
                },
                tableConfig: {
                    fixedWidth: 150,
                    scrollWidth: 1000,
                    tableCol: [
                        {
                            type: "",
                            name: "客户名称",
                            dataField: "companyName",
                            width: 150,
                            isShow: true,
                            isFixed: true,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "零件编号",
                            dataField: "materialNo",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "零件中文名",
                            dataField: "materialName",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "零件分类",
                            dataField: "materialType",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "仓库编码",
                            dataField: "warehouseCode",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "库区",
                            dataField: "areaCode",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "储位",
                            dataField: "locationCode",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "可用库存",
                            dataField: "enableNum",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "计量单位",
                            dataField: "unit",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "库存类型",
                            dataField: "statusShow",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "保管员",
                            dataField: "keeper",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        }
                    ]
                },
                search: function (curpage) {
                    getWarehouseStockList(curpage);
                },
                reset: function () {
                    $scope.warehouseStock.condition = {};
                },
                switchToolbar: function (toolbar) {
                    if (!!$scope.warehouseStock.toolbarConfig["show" + toolbar]) {
                        $scope.warehouseStock.toolbarConfig["show" + toolbar] = false;
                    } else {
                        $timeout(function () {
                            $scope.warehouseStock.toolbarConfig["show" + toolbar] = true;
                        });
                    }
                },
                setFixedWidth: function () {
                    $scope.warehouseStock.tbStyle = {
                        billFixedWidth: {left: $scope.warehouseStock.tableConfig.fixedWidth + "px"}
                    };
                },
                toSort: function (config, index) {
                    ybWidget.ToSort(config, index);
                },
                selectRow: function (index) {
                    $scope.warehouseStock.markInfo.cueRow = index
                },
                tdClick: function (item, fnType) {
                    switch (fnType) {
                        default:
                            break;
                    }
                },
            };
            // 价格查询 Start
            $scope.PriceStock = {
                supplierOrgId: '',
                markInfo: {
                    curRow: 0
                },
                markInfoLack: {
                    allCheckedBill: false,
                    curRow: -1
                },                   
                pageInfo: {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                },
                dataList: [],
                condition: {},
                toolbarConfig: {
                    showAdvanceSearch: false,
                    showColConfig: false,
                    showMateriel: false
                },
                tableConfig: {
                    fixedWidth: -1,
                    scrollWidth: 1400,
                    tableCol: [
                        {
                            type: "",
                            name: "零件编号",
                            dataField: "materialsNo",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "零件中文名",
                            dataField: "materialsDesc",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "方案名称",
                            dataField: "activityName",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "价格",
                            dataField: "activityPriceShow",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "全国统一价",
                            dataField: "price",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "产品吨位",
                            dataField: "productTonner",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "是否在制",
                            dataField: "isMarkingShow",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                    ]
                },
                search: function (curpage) {
                    getPriceStockList(curpage);
                },
                reset: function () {
                    $scope.PriceStock.condition = {};
                },
                switchToolbar: function (toolbar) {
                    if (!!$scope.PriceStock.toolbarConfig["show" + toolbar]) {
                        $scope.PriceStock.toolbarConfig["show" + toolbar] = false;
                    } else {
                        $timeout(function () {
                            $scope.PriceStock.toolbarConfig["show" + toolbar] = true;
                        });
                    }
                },
                setFixedWidth: function () {
                    $scope.PriceStock.tbStyle = {
                        billFixedWidth: {left: $scope.PriceStock.tableConfig.fixedWidth + "px"}
                    };
                },
                toSort: function (config, index) {
                    ybWidget.ToSort(config, index);
                },
                selectRow: function (index) {
                    $scope.PriceStock.markInfo.cueRow = index
                },
                tdClick: function (item, fnType) {
                    switch (fnType) {
                        default:
                            break;
                    }
                },
                selectAll: function (list, type) {
                    angular.forEach(list, function (item) {
                        item.isChecked = $scope.PriceStock.markInfo["allChecked" + type];
                    });   
                    $scope.CKCheck = list.filter(item => item.isChecked == true)
                    $scope.exportParam.sectionParam.list = [];
                    $scope.exportParam.sectionParam.list = $scope.CKCheck
                }                  
            };

            $scope.viewWarehouseStock = function (supplierOrgId) {
                $scope.warehouseStock.supplierOrgId = supplierOrgId;
                $scope.warehouseStock.reset();
                $scope.warehouseStock.dataList = [];
                
                $scope.changeTab(3);
                $scope.warehouseStock.setFixedWidth();
            };
            $scope.viewPriceStock = function (item) {
                const { supplierOrgId, outsideFlag } = item;
                 $scope.exportParam.sectionParam.supplierOrgId = supplierOrgId
                if(window.JCHK_ORG_IDs.includes(supplierOrgId) || outsideFlag == 1){
                    // 当供货商是进出口、境外企业时，查询价格 隐藏表格列
                    ybWidget.GetTableCol($scope.PriceStock.tableConfig.tableCol, 'activityName').isShow = false;
                    ybWidget.GetTableCol($scope.PriceStock.tableConfig.tableCol, 'price').isShow = false;
                } else {
                    ybWidget.GetTableCol($scope.PriceStock.tableConfig.tableCol, 'activityName').isShow = true;
                    ybWidget.GetTableCol($scope.PriceStock.tableConfig.tableCol, 'price').isShow = true;
                }
                // 进出口 如果供货商是徐工挖机 全国统一价隐藏
                if (window.WJ_ORG_IDs.includes(item.supplierOrgId) && $scope.isJCHK) {
                    ybWidget.GetTableCol($scope.PriceStock.tableConfig.tableCol, 'price').isShow = false;
                }else if(!window.WJ_ORG_IDs.includes(item.supplierOrgId) && $scope.isJCHK) {
                    ybWidget.GetTableCol($scope.PriceStock.tableConfig.tableCol, 'price').isShow = true;
                }
                $scope.PriceStock.supplierOrgId = supplierOrgId;
                $scope.PriceStock.reset();
                $scope.PriceStock.dataList = [];
                getPriceStockList().then(function () {
                    $scope.changeTab(5);
                    $scope.PriceStock.setFixedWidth();
                });
            };
            function getPriceStockList(curPage) {
                var defer = $q.defer();
                var service = "device";
                var reqUrl = "/marketingActivity/supplierPriceScheme";
                if (!!curPage) {
                    $scope.PriceStock.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.PriceStock.pageInfo.pageSize,
                    pageNum: $scope.PriceStock.pageInfo.curPage,
                    filters: {
                        ...$scope.PriceStock.condition,
                        supplierOrgId: $scope.PriceStock.supplierOrgId,
                        countryVOS: $scope.curUser.associateCountryVOS,
                        jckFlag: $scope.isJCHK
                    }
                };
                $scope.AllExport = angular.copy(param.filters)
                // 批量查询用,分割
                if (param.filters.materialsNs) {                    
                    let materialsNos = JSON.parse(JSON.stringify(param.filters.materialsNs))
                    // 如为数组终止
                    if (Array.isArray(materialsNos))  return 
                    param.filters.materialsNos = materialsNos.split(',')
                    param.filters.materialsNos = materialsNos.split('\n')
                }   
                // 高级搜索用del删除时有缓存,重新清理
                if (!param.filters.materialsNs) param.filters.materialsNos = undefined
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.PriceStock.pageInfo.totalRows = resp.value.total;
                    $scope.PriceStock.pageInfo.totalPages = resp.value.pages;
                    angular.forEach(resp.value.list, function (item) {
                        item.activityPriceShow = $filter("decimalCover")(item.activityPrice, 2);
                        item.isMarkingShow =  $filter("ProgressStatus")(item.isMarking,"dbFlag");
                        item.isChecked = false

                    });
                    $scope.PriceStock.dataList = resp.value.list;
                    defer.resolve();
                });
                return defer.promise;
            }
            $scope.changeExport = function () {
                var param = {
                    filters: {
                        ...$scope.PriceStock.condition,
                        supplierOrgId: $scope.PriceStock.supplierOrgId,
                        countryVOS: $scope.curUser.associateCountryVOS,
                        jckFlag: $scope.isJCHK
                    }
                };
                $scope.AllExport = angular.copy(param.filters)
            }
            function getWarehouseStockList(curPage) {
                var defer = $q.defer();
                var service = "device";
                var reqUrl = "/warehouseInventory/enterpriseList";
                var param = {};
                param.pageSize = $scope.warehouseStock.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.warehouseStock.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.warehouseStock.pageInfo.curPage;
                param.filters = $scope.warehouseStock.condition;
                param.filters.supplierOrgId = $scope.warehouseStock.supplierOrgId;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.statusShow = $filter('storageType')(item.status);
                    });
                    $scope.warehouseStock.pageInfo.totalRows = resp.value.total;
                    $scope.warehouseStock.pageInfo.totalPages = resp.value.pages;
                    $scope.warehouseStock.dataList = resp.value.list;
                    defer.resolve();
                });
                return defer.promise;
            }

            // 仓库库存 End

            // 客户库存 Start
            $scope.customerStock = {
                supplierOrgId: '',
                markInfo: {
                    allChecked: false,
                    curRow: 0
                },
                pageInfo: {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                },
                dataList: [],
                condition: {},
                toolbarConfig: {
                    showAdvanceSearch: false,
                    showColConfig: false,
                    showMateriel: false
                },
                tableConfig: {
                    fixedWidth: 300,
                    scrollWidth: 1000,
                    tableCol: [
                        {
                            type: "",
                            name: "客户名称",
                            dataField: "companyName",
                            width: 150,
                            isShow: true,
                            isFixed: true,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "零件编号",
                            dataField: "materialNo",
                            width: 150,
                            isShow: true,
                            isFixed: true,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "零件中文名",
                            dataField: "materialName",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "零件分类",
                            dataField: "materialType",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "仓库编码",
                            dataField: "warehouseCode",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "库区",
                            dataField: "areaCode",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "储位",
                            dataField: "locationCode",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "库存数量",
                            dataField: "amount",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "计量单位",
                            dataField: "unit",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "库存类型",
                            dataField: "statusShow",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "保管员",
                            dataField: "keeper",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: true,
                            sortType: ""
                        }
                    ]
                },
                search: function (curpage) {
                    getCustomerStockList(curpage);
                },
                reset: function () {
                    $scope.customerStock.condition = {};
                },
                switchToolbar: function (toolbar) {
                    if (!!$scope.customerStock.toolbarConfig["show" + toolbar]) {
                        $scope.customerStock.toolbarConfig["show" + toolbar] = false;
                    } else {
                        $timeout(function () {
                            $scope.customerStock.toolbarConfig["show" + toolbar] = true;
                        });
                    }
                },
                selectDealer: function () {
                    var param = {
                        name: "客户",
                        type: "dealer",
                        supplierOrgId: $scope.customerStock.supplierOrgId,
                        isPlatCompany: 1
                    };
                    ybWidget.SingleSelector(param).then(function (data) {
                        $scope.customerStock.condition = {
                            orgName: data.companyName,
                           dealerOrgId: data.platOrgId
                        };
                        getCustomerStockList(1);
                    })
                },
                clearDealer: function () {
                    $scope.customerStock.condition = {
                        orgName: "",
                        dealerOrgId: ""
                    };
                    $scope.customerStock.dataList = [];
                },
                setFixedWidth: function () {
                    $scope.customerStock.tbStyle = {
                        billFixedWidth: {left: $scope.customerStock.tableConfig.fixedWidth + "px"}
                    };
                },
                toSort: function (config, index) {
                    ybWidget.ToSort(config, index);
                },
                selectRow: function (index) {
                    $scope.customerStock.markInfo.cueRow = index
                },
                tdClick: function (item, fnType) {
                    switch (fnType) {
                        default:
                            break;
                    }
                },
            };

            $scope.viewCustomerStock = function (supplierOrgId) {
                $scope.customerStock.supplierOrgId = supplierOrgId;
                $scope.customerStock.reset();
                $scope.customerStock.dataList = [];
                $scope.changeTab(4);
                $scope.customerStock.setFixedWidth();
            };

            $scope.selectSingleLack = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.PriceStock.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.PriceStock.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.PriceStock.markInfo["allChecked" + type] = true;
                    }
                }
                $scope.CKCheck = list.filter(item => item.isChecked == true)
                $scope.exportParam.sectionParam.list = [];
                $scope.exportParam.sectionParam.list = $scope.CKCheck
            };            

            function getCustomerStockList(curPage) {
                var defer = $q.defer();
                var service = "device";
                var reqUrl = "/warehouseInventory/listForDealer";
                var param = {};
                param.pageSize = $scope.customerStock.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.customerStock.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.customerStock.pageInfo.curPage;
                param.filters = $scope.customerStock.condition;
                param.filters.supplierOrgId = $scope.customerStock.supplierOrgId;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.statusShow = $filter('storageType')(item.status);
                    });
                    $scope.customerStock.pageInfo.totalRows = resp.value.total;
                    $scope.customerStock.pageInfo.totalPages = resp.value.pages;
                    $scope.customerStock.dataList = resp.value.list;
                    defer.resolve();
                });
                return defer.promise;
            }

            // 客户库存 End
            const masterOrgIdsTemp = $scope.curOrg.masterOrgIds || [];
            const isCHYDlsh = masterOrgIdsTemp.some(cmo => window.CHY_ORG_IDs.includes(cmo)); // 当前登录账号是基础代理商
            if (isCHYDlsh) {
                $scope.isCHYDlsh = true;
            }

            $scope.selectCompany = function () {
                if (!$scope.detailInfo.supplierClassId) {
                    ybWidget.ShowMessage("请先选择供货商分类", MESG_TYPE.Error);
                    return;
                }
                const options = {
                    title: '供货商',
                    service: 'device',
                    url: '/company/list',
                    otherParams: { companyType: $scope.detailInfo.supplierClassId },
                    keyWords: [{label: '供货商代码', field: 'supplierNoLike'}, {label: '供货商名称', field: 'companyNameLike'}],
                    fields: [
                        {
                            name: '供货商代码',
                            dataField: 'supplierNo',
                            width: "25%"
                        },
                        {
                            name: '供货商名称',
                            dataField: 'companyName',
                            width: "50%"
                        },
                        {
                            name: '组织机构代码',
                            dataField: 'orgCode',
                            width: "25%"
                        },
                    ]
                }
                ybWidget.DataSelector(options).then(function (data) {
                    $scope.detailInfo.supplierCode = data.supplierNo;
                    $scope.detailInfo.companyName = data.companyName;
                })
            };

            $scope.clearCompany = function() {
                $scope.detailInfo.supplierCode = '';
                $scope.detailInfo.companyName = '';
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);
